/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.upc.clase.demo.dao.impl;

import edu.upc.clase.demo.dao.LocalDao;
import edu.upc.clase.demo.entity.Local;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.stereotype.Repository;
/**
 *

 */
@Repository
public class LocalDaoImpl extends SimpleJdbcDaoSupport implements LocalDao{
  private static Logger log = LoggerFactory.getLogger(LocalDaoImpl.class);
  
      @Autowired
       public LocalDaoImpl (DataSource dataSource) {
       log.info("Asignando el dataSource");
       setDataSource(dataSource);    
    }
      @Override
      public Integer insertar(Local local) {
        getJdbcTemplate().update(
                "insert into Local (codLocal,nombreLocal,direccionLocal) values (?, ?, ?)",
                local.getCodLocal(),local.getNombreLocal(),local.getDireccionLocal());
        return getSimpleJdbcTemplate().queryForInt("select last_insert_codLocal()");
    }
      @Override
      public void actualizar(Local local) {
        getJdbcTemplate().update(
                "update Local set nombreLocal = ?, direccionLocal = ? where codLocal = ?",
                local.getNombreLocal(),local.getDireccionLocal(),local.getCodLocal());
    }

    @Override
    public void eliminar(Local local) {
        getJdbcTemplate().update(
                "delete from Local where codLocal = ?",local.getCodLocal() );
    }
    @Override
    public Local buscar(String nombLocal) {
        try {
            return getSimpleJdbcTemplate().queryForObject(
                    "select codLocal, nombreLocal,direccionLocal from Local where nombLocal=?",
                    new BeanPropertyRowMapper<Local>(Local.class), nombLocal);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    @Override
    public List<Local> buscarTodos() {
        return getSimpleJdbcTemplate().query(
                "select codLocal, nombreLocal,direccionLocal from Local",
                new BeanPropertyRowMapper<Local>(Local.class));
    }
}
